<?php

namespace pmvc\tx;

/**
 * A transaction wrapper that keeps track of how many
 * start() and commit() calls there are to ensure that
 * many components participating in the same transaction
 * don't accidentally close the transaction before it
 * should be closed.
 *
 */
class JoinedTransaction
	implements Transaction {

	private $tx;
	private $refCount = 0;

    public function __construct(Transaction $tx) {
    	$this->tx 		= $tx;
    	$this->refCount	= 0;
    }

	public function start() {
		if ($this->refCount==0) {
			$this->tx->start();
		}
		$this->refCount++;
	}

	public function commit() {
		$this->refCount--;
		if ($this->refCount==0) {
			$this->tx->commit();
		}
	}

	public function forceCommit() {
		$this->refCount = 0;
		$this->tx->commit();
	}

	public function rollback() {
		$this->tx->rollback();
	}

	public function setAutoCommit($autoCommit) {
		$this->tx->setAutoCommit($autoCommit);
	}
	
	public function isAutoCommit() {
		return $this->tx->isAutoCommit();
	}

	public function isCommited() {
		return $this->tx->isCommited();
	}

	public function isRolledBack() {
		return $this->tx->isRolledBack();
	}

	public function getTransaction() {
		return $this->tx;
	}
	
}
?>